{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be8cc818",
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "# jupyter的各种使用方法\n",
    "# jupyterlab是jupyter notebook的升级版，更加强大"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe9c69ee-f28b-48b9-9cf6-48bb02fc9e79",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 安装jupyterlab\n",
    "pip3 install jupyterlab  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1819e0bb",
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "# 升级jupyter\n",
    "pip install -U jupyter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c115e2d8-b40d-4506-bdc3-0a6a893909cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 安装jupyterlab中文语言包\n",
    "pip3 install jupyterlab-language-pack-zh-CN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9bb2de2-c46e-4456-87f3-c6d53c1c4d62",
   "metadata": {},
   "outputs": [],
   "source": [
    "# jupyter内核列表网址\n",
    "'https://github.com/jupyter/jupyter/wiki/Jupyter-kernels'\n",
    "\n",
    "# 安装bash内核，用于支持bash命令行\n",
    "pip3 install bash_kernel\n",
    "python3 -m bash_kernel.install"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ac3efe4-257b-4fcc-a6b1-c7bd18aa9405",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 启动jupyterlab\n",
    "jupyter-lab \\\n",
    "\n",
    "# jupyter常用的启动参数\n",
    "--ip=192.168.0.1 \\ #指定jupyter运行的ip，在云服务器运行需指定内网ip，否则无法打开jupyter\n",
    "--port 9999 \\ #指定jupyter的端口，默认为8888，可以同时运行多个jupyter使用不同的端口即可\n",
    "--allow-root \\ #允许root用户运行jupyter服务器\n",
    "--no-browser \\ #不在浏览器打开jupyter\n",
    "--autoreload \\ #自动加载网络应用和导入的python包\n",
    "--generate-config \\ #生成默认配置文件\n",
    "--notebooke-dir=/var \\ #指定笔记本和内核启动目录，默认为当前目录\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a557d36b-1c00-4492-9f2e-55f455f423aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 显示所有运行中的jupyter和对应token及目录\n",
    "jupyter server list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e91dadbc",
   "metadata": {},
   "source": [
    "### jupyter的高级用法\n",
    "###### Magic命令：可以运行一些特殊的命令Magic 命令的前面带有一个%或两个%，一个%代表行 Magic 命令，两个%代表单元格 Magic 命令。行 Magic 命令仅应用于编写 Magic 命令时所在的行，而单元格 Magic 命令应用于整个单元格。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8910b7c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": {
       "cell": {
        "!": "OSMagics",
        "HTML": "Other",
        "SVG": "Other",
        "bash": "Other",
        "capture": "ExecutionMagics",
        "cmd": "Other",
        "debug": "ExecutionMagics",
        "file": "Other",
        "html": "DisplayMagics",
        "javascript": "DisplayMagics",
        "js": "DisplayMagics",
        "latex": "DisplayMagics",
        "markdown": "DisplayMagics",
        "perl": "Other",
        "prun": "ExecutionMagics",
        "pypy": "Other",
        "python": "Other",
        "python2": "Other",
        "python3": "Other",
        "ruby": "Other",
        "script": "ScriptMagics",
        "sh": "Other",
        "svg": "DisplayMagics",
        "sx": "OSMagics",
        "system": "OSMagics",
        "time": "ExecutionMagics",
        "timeit": "ExecutionMagics",
        "writefile": "OSMagics"
       },
       "line": {
        "alias": "OSMagics",
        "alias_magic": "BasicMagics",
        "autoawait": "AsyncMagics",
        "autocall": "AutoMagics",
        "automagic": "AutoMagics",
        "autosave": "KernelMagics",
        "bookmark": "OSMagics",
        "cd": "OSMagics",
        "clear": "KernelMagics",
        "cls": "KernelMagics",
        "colors": "BasicMagics",
        "conda": "PackagingMagics",
        "config": "ConfigMagics",
        "connect_info": "KernelMagics",
        "copy": "Other",
        "ddir": "Other",
        "debug": "ExecutionMagics",
        "dhist": "OSMagics",
        "dirs": "OSMagics",
        "doctest_mode": "BasicMagics",
        "echo": "Other",
        "ed": "Other",
        "edit": "KernelMagics",
        "env": "OSMagics",
        "gui": "BasicMagics",
        "hist": "Other",
        "history": "HistoryMagics",
        "killbgscripts": "ScriptMagics",
        "ldir": "Other",
        "less": "KernelMagics",
        "load": "CodeMagics",
        "load_ext": "ExtensionMagics",
        "loadpy": "CodeMagics",
        "logoff": "LoggingMagics",
        "logon": "LoggingMagics",
        "logstart": "LoggingMagics",
        "logstate": "LoggingMagics",
        "logstop": "LoggingMagics",
        "ls": "Other",
        "lsmagic": "BasicMagics",
        "macro": "ExecutionMagics",
        "magic": "BasicMagics",
        "matplotlib": "PylabMagics",
        "mkdir": "Other",
        "more": "KernelMagics",
        "notebook": "BasicMagics",
        "page": "BasicMagics",
        "pastebin": "CodeMagics",
        "pdb": "ExecutionMagics",
        "pdef": "NamespaceMagics",
        "pdoc": "NamespaceMagics",
        "pfile": "NamespaceMagics",
        "pinfo": "NamespaceMagics",
        "pinfo2": "NamespaceMagics",
        "pip": "PackagingMagics",
        "popd": "OSMagics",
        "pprint": "BasicMagics",
        "precision": "BasicMagics",
        "prun": "ExecutionMagics",
        "psearch": "NamespaceMagics",
        "psource": "NamespaceMagics",
        "pushd": "OSMagics",
        "pwd": "OSMagics",
        "pycat": "OSMagics",
        "pylab": "PylabMagics",
        "qtconsole": "KernelMagics",
        "quickref": "BasicMagics",
        "recall": "HistoryMagics",
        "rehashx": "OSMagics",
        "reload_ext": "ExtensionMagics",
        "ren": "Other",
        "rep": "Other",
        "rerun": "HistoryMagics",
        "reset": "NamespaceMagics",
        "reset_selective": "NamespaceMagics",
        "rmdir": "Other",
        "run": "ExecutionMagics",
        "save": "CodeMagics",
        "sc": "OSMagics",
        "set_env": "OSMagics",
        "store": "StoreMagics",
        "sx": "OSMagics",
        "system": "OSMagics",
        "tb": "ExecutionMagics",
        "time": "ExecutionMagics",
        "timeit": "ExecutionMagics",
        "unalias": "OSMagics",
        "unload_ext": "ExtensionMagics",
        "who": "NamespaceMagics",
        "who_ls": "NamespaceMagics",
        "whos": "NamespaceMagics",
        "xdel": "NamespaceMagics",
        "xmode": "BasicMagics"
       }
      },
      "text/plain": [
       "Available line magics:\n",
       "%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cd  %clear  %cls  %colors  %conda  %config  %connect_info  %copy  %ddir  %debug  %dhist  %dirs  %doctest_mode  %echo  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %macro  %magic  %matplotlib  %mkdir  %more  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %ren  %rep  %rerun  %reset  %reset_selective  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode\n",
       "\n",
       "Available cell magics:\n",
       "%%!  %%HTML  %%SVG  %%bash  %%capture  %%cmd  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile\n",
       "\n",
       "Automagic is ON, % prefix IS NOT needed for line magics."
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示全部可用的magic命令\n",
    "%lsmagic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6ecd2c36",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用timeit命令统计运行时间\n",
    "\n",
    "%%timeit\n",
    "\n",
    "import random\n",
    "num=0\n",
    "for i in range(100):\n",
    "    num+=random.randint(1,100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "52fe5cd0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "env: tmp=123\n"
     ]
    }
   ],
   "source": [
    "# 使用env命令管理notebook的环境变量\n",
    "%env tmp=123\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83cf8503",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用run命令运行python代码\n",
    "%run jupyter/python3/pythonscript/pypi.py\n",
    "\n",
    "# 使用!python也可以运行\n",
    "!python jupyter/python3/pythonscript/pypi.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b63d78e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "UsageError: Line magic function `%%writefile` not found.\n"
     ]
    }
   ],
   "source": [
    "# 使用writefile命令导出cell的内容到文件\n",
    "%%writefile test.py \n",
    "\n",
    "print('hello world')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "34ced099",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: no such file, variable, URL, history range or macro\n"
     ]
    }
   ],
   "source": [
    "# 使用pycat命令把文件内容在cell中显示\n",
    "%pycat test.py  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ea69964e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31mSignature:\u001b[0m \u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mDocstring:\u001b[0m\n",
      "Return random integer in range [a, b], including both end points.\n",
      "        \n",
      "\u001b[1;31mSource:\u001b[0m   \n",
      "    \u001b[1;32mdef\u001b[0m \u001b[0mrandint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\n",
      "\u001b[0m        \u001b[1;34m\"\"\"Return random integer in range [a, b], including both end points.\n",
      "        \"\"\"\u001b[0m\u001b[1;33m\n",
      "\u001b[0m\u001b[1;33m\n",
      "\u001b[0m        \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mFile:\u001b[0m      c:\\programdata\\miniconda3\\lib\\random.py\n",
      "\u001b[1;31mType:\u001b[0m      method\n"
     ]
    }
   ],
   "source": [
    "# 使用?查看某个对象的属性和方法,单个?显示简略信息,双个?显示详细信息\n",
    "import random\n",
    "??random.randint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e0ad1c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使jupyter自动显示所有的变量\n",
    "!pip install pydataset\n",
    "\n",
    "form IPython.core.interactiveshell import InteractiveShell  # 导入IPython的InteractiveShell类\n",
    "InteractiveShell.ast_node_interactivity = \"all\"  # 设置ast_node_interactivity为all，即所有的ast节点都会被显示\n",
    "\n",
    "from pydataset import data\n",
    "val=data('quakes')\n",
    "val.head()\n",
    "val.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c358b634",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用ipdb调试python代码(需要使用pip install ipdb安装)\n",
    "import ipdb\n",
    "\n",
    "def main():\n",
    "    num1=100\n",
    "    num2=200\n",
    "    print('第二次执行')\n",
    "    ipdb.set_trace() # 添加一个断点\n",
    "    print('hello world')\n",
    "    \n",
    "print('第一次执行')\n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aeec49e6",
   "metadata": {},
   "source": [
    "- 程序运行后,会出现交互框,输入命令后按回车进行调试,命令都是一个字母\n",
    "- Enter:重复上次命令\n",
    "- n:转到下一行(不会进入到函数中)\n",
    "- c:跳转到最后或下一个断点\n",
    "- p:打印变量,例如:p num1 num2\n",
    "- b:动态添加断点,例如:b num1\n",
    "- l:查找当前位于哪里\n",
    "- s:进入子程序,进入被调用的函数体\n",
    "- r:运行直到子程序结束\n",
    "- !:python命令\n",
    "- h:帮助\n",
    "- a(main):打印当前函数的参数\n",
    "- j(line):让程序跳转到指定的行\n",
    "- 动态添加变量,例如:num1='value'\n",
    "- q:退出调试"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d58fb73-4297-4483-816e-a18c0b276b69",
   "metadata": {},
   "source": [
    "## jupyter快捷键用法\n",
    "---\n",
    "#### 命令行模式\n",
    "1. 选择前一个cell  \n",
    "    <kbd>↑</kbd>或<kbd>k</kbd>\n",
    "\n",
    "2. 选择后一个cell  \n",
    "    <kbd>↓</kbd>或<kbd>j</kbd>\n",
    "\n",
    "3. 运行当前cell\n",
    "    <kbd>Ctrl</kbd>+<kbd>Enter</kbd>\n",
    "\n",
    "4. 执行当前cell并跳转到下一个cell    \n",
    "    <kbd>Shift</kbd>+<kbd>Enter</kbd>\n",
    "\n",
    "5. 删除当前cell   \n",
    "    <kbd>D</kbd>+<kbd>D</kbd>\n",
    "    \n",
    "6. 回复删除的cell  \n",
    "    <kbd>Z</kbd>\n",
    "\n",
    "7. 在当前cell上方插入cell  \n",
    "    <kbd>a</kbd>\n",
    "\n",
    "8. 在当前cell下方插入cell  \n",
    "    <kbd>b</kbd>\n",
    "\n",
    "9. 复制当前cell  \n",
    "    <kbd>C</kbd>\n",
    "\n",
    "10. 粘贴到当前cell的下方  \n",
    "    <kbd>V</kbd>\n",
    "\n",
    "11. 切换当前cell的类型为code  \n",
    "    <kbd>Y</kbd>\n",
    "\n",
    "12. 切换当前cell的类型为markdown  \n",
    "    <kbd>M</kbd>\n",
    "\n",
    "13. 进入编辑模式  \n",
    "    <kbd>Enter</kbd>\n",
    "\n",
    "14. 显示/隐藏cell输出  \n",
    "    <kbd>O</kbd>\n",
    "\n",
    "#### 编辑模式\n",
    "1. 退出编辑模式    \n",
    "    <kbd>Esc</kbd>\n",
    "\n",
    "2. 撤销上一步操作    \n",
    "    <kbd>Ctrl</kbd>+<kbd>Z</kbd>\n",
    "\n",
    "3. 重做上一步操作    \n",
    "    <kbd>Ctrl</kbd>+<kbd>Y</kbd>\n",
    "\n",
    "4. 添加缩进  \n",
    "    <kbd>Tab</kbd>\n",
    "\n",
    "5. 删除缩进  \n",
    "    <kbd>Shift</kbd>+<kbd>Tab</kbd>\n",
    "\n",
    "6. 取消/添加注释整行  \n",
    "    <kbd>Ctrl</kbd>+<kbd>/</kbd>\n",
    "\n",
    "7. 删除整行  \n",
    "    <kbd>Ctrl</kbd>+<kbd>D</kbd>\n",
    "\n",
    "8. 移动到行首  \n",
    "    <kbd>Home</kbd>或<kbd>Ctrl</kbd>+<kbd>←</kbd>\n",
    "\n",
    "9. 移动到行尾  \n",
    "    <kbd>End</kbd>或<kbd>Ctrl</kbd>+<kbd>→</kbd>\n",
    "\n",
    "10. 执行当前cell,会退出编辑模式    \n",
    "    <kbd>Ctrl</kbd>+<kbd>Enter</kbd>\n",
    "\n",
    "11. 执行当前cell并跳转到下一个cell      \n",
    "    <kbd>Shift</kbd>+<kbd>Enter</kbd>\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "ecf5722fdaf1897a315d257d89d94520bfcaa453217d5becf09b39e73618b0de"
  },
  "kernelspec": {
   "display_name": "Bash",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
